package com.SmartWallet;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.*;
import android.graphics.*;
import android.widget.TextView;
import android.widget.Toast;

import java.util.*;


import com.SmartWallet.models.BudgetList;
import com.SmartWallet.models.ListEntry;

public class ListViewActivity extends Activity{
	static HashMap<String,ListEntry> entries;
	static ArrayList<ListEntry> entryList;
	ArrayList<TextView> typeList, costList;
	ArrayList<LinearLayout> layoutList;
	ArrayList<TableRow> rowList;
	ArrayList<View> viewList;
	BudgetList list;
	static ArrayList<String> entryNames, entryTypes, entryCost;
	View v;
	TableLayout tl;
    /** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  entries = new HashMap<String,ListEntry>();
	
	  
		
		viewList = new ArrayList<View>();
		//SmartWalletActivity.lists.get(SmartWalletActivity.selected).updateExpenses();
		list = 	SmartWalletActivity.lists.get(SmartWalletActivity.selected);
		
	  
	  
	  
	  entryNames = new ArrayList<String>();
	  entryTypes = new ArrayList<String>();
	  entryCost = new ArrayList<String>(); 
	 entryList =  list.getEntries();
	 for(int i = 0; i < entryList.size(); i++)
	 {
		 entryNames.add(entryList.get(i).getName());
		 entryTypes.add(entryList.get(i).getType());
		 entryCost.add(" $" + entryList.get(i).getCost());
		 entries.put(entryList.get(i).getName(), entryList.get(i));
	 }
	 v = new View(this);
		v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
		v.setBackgroundColor(Color.rgb(51, 51, 51));
		tl = new TableLayout(this);
	  updateView();
	  tl.addView(v);
		setContentView(tl);
	  

	  
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.entry_menu, menu);
	    return true;
	}
	
	
	
	public void updateView()
	{
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(140, 30);
        layoutParams.setMargins(24, 0, 24, 0);
		rowList = new ArrayList<TableRow>();
		typeList= new ArrayList<TextView>();
		costList = new ArrayList<TextView>();	
		layoutList = new ArrayList<LinearLayout>();
		
		TableRow tr = new TableRow(this);
		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);
		TextView listName = new TextView(this);
		TextView budget = new TextView(this);
		TextView expenses = new TextView(this);
		TextView balance = new TextView(this);
		listName.setText(list.getName());
		listName.setTextSize(20);
		budget.setText("Budget: $" + list.getBudget());
		budget.setTextSize(20);
		expenses.setText("Expenses: $" + list.getExpenses());
		expenses.setTextSize(20);
		if(list.getBalance() < list.getWarning() && list.getBalance() > 0)
		{
			balance.setTextColor(Color.YELLOW);
			balance.setText("Balance: $" + list.getBalance());
			balance.setTextSize(20);
			Toast.makeText(getApplicationContext(), "Warning: Approaching Limit",
			          Toast.LENGTH_LONG).show();
		}
		else if(list.getBalance() <= 0)
		{
			balance.setTextColor(Color.RED);
			balance.setText("Balance: $" + list.getBalance());
			balance.setTextSize(20);
			
			Toast.makeText(getApplicationContext(), "You have exceded your limit",
			          Toast.LENGTH_LONG).show();
		}
		else
		{
			
			balance.setText("Balance: $" + list.getBalance());
			balance.setTextSize(20);
			
		}
		ll.addView(budget);
		ll.addView(expenses);
		ll.addView(balance);
		tr.addView(ll);
		tl.addView(tr);
		View line = new View(this);
		line.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
		line.setBackgroundColor(Color.rgb(51, 51, 51));
		TableRow lineRow = new TableRow(this);
		lineRow.addView(line);
		tl.addView(lineRow);
		TableRow descriptRow = new TableRow(this);
		LinearLayout descriptLay = new LinearLayout(this);
		descriptLay.setOrientation(LinearLayout.HORIZONTAL);
		TextView name = new TextView(this);
		TextView type = new TextView(this);
		TextView cost = new TextView(this);
		name.setText("Name");
		type.setText("Type");
		cost.setText("Cost");
		descriptLay.addView(name,layoutParams);
		descriptLay.addView(type,layoutParams);
		descriptLay.addView(cost,layoutParams);
		descriptRow.addView(descriptLay);
		tl.addView(descriptRow);
		
		View line2 = new View(this);
		line2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
		line2.setBackgroundColor(Color.rgb(51, 51, 51));
		TableRow lineRow2 = new TableRow(this);
		lineRow2.addView(line2);
		tl.addView(lineRow2);
		
		
		
		/*tr.setOnClickListener(new  OnClickListener() {

	        
	        public void onClick(View v) {
	           // v.setBackgroundColor(Color.GRAY);
	            // TODO Auto-generated method stub
	            Toast.makeText(getApplicationContext(), entryList.get(0).getComment(),
				          Toast.LENGTH_LONG).show();

	        }
	    });*/
		for(int i = 0; i < entryNames.size(); i++)
		{
			rowList.add(new TableRow(this));
			viewList.add(new View(this));
			layoutList.add(new LinearLayout(this));
			
			
		}
		for(int i = 0; i < entryNames.size(); i++)
		{
			TextView tv = new TextView(this);
			TextView tv2 = new TextView(this);
			TextView tv3 = new TextView(this);
			tv.setText(entryNames.get(i));
			tv2.setText(entryTypes.get(i));
			tv3.setText(entryCost.get(i));
			layoutList.get(i).setOrientation(LinearLayout.HORIZONTAL);
			layoutList.get(i).addView(tv,layoutParams);
			layoutList.get(i).addView(tv2,layoutParams);
			layoutList.get(i).addView(tv3,layoutParams);
			rowList.get(i).addView(layoutList.get(i));
			
			
		}
		for(int i = 0; i < entryNames.size(); i++)
		{
			tl.addView(rowList.get(i));
			
			viewList.get(i).setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
			viewList.get(i).setBackgroundColor(Color.rgb(51, 51, 51));
			tl.addView(viewList.get(i));
			
			
			
		}
		
		
		
		/*TableRow tr2= new TableRow(this);
		
		
		TextView tv2 = new TextView(this);
		tv2.setText(entryNames.get(0));
		tr.addView(tv); 
		tr2.addView(tv2);*/
		
		//tl.addView(tr2);
		
		
	}
	public void updateList()
	{
		SmartWalletActivity.lists.get(SmartWalletActivity.selected).updateExpenses();
		list = 	SmartWalletActivity.lists.get(SmartWalletActivity.selected);
		
		  
		viewList = new ArrayList<View>();
		  
		  entryNames = new ArrayList<String>();
		  entryTypes = new ArrayList<String>();
		  entryCost = new ArrayList<String>(); 
		 entryList =  list.getEntries();
		 for(int i = 0; i < entryList.size(); i++)
		 {
			 entryNames.add(entryList.get(i).getName());
			 entryTypes.add(entryList.get(i).getType());
			 entryCost.add(" $" + entryList.get(i).getCost());
			 entries.put(entryList.get(i).getName(), entryList.get(i));
		 }
		 
	}
	
	
	@Override
    protected void onStart() {
        super.onStart();
        tl.removeAllViews();
        // The activity is about to become visible.
        updateList();
       updateView();
       tl.addView(v);
	   setContentView(tl);
        
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
        //updateView();
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void onStop() {
        super.onStop();
        
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.new_entry:
            	Intent myIntent = new Intent(tl.getContext(), NewEntryActivity.class);
                startActivityForResult(myIntent, 0);		
				

				

				//Start next activity
				//ListViewActivity.this.startActivity(intent); 

				//Start next activity
				
                return true;
            case R.id.send:
            	Toast.makeText(getApplicationContext(), "Sent to PC",
  			          Toast.LENGTH_LONG).show();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

